package sealte.hvpms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sealte.hvpms.dao.UserDAO;
import sealte.hvpms.dao.impl.UserDAOImpl;

public class GetPassword extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html"); 
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String gpQues = request.getParameter("gp_ques");
		String gpAnsw = request.getParameter("gp_answ");
		UserDAO ub = new UserDAOImpl();
		boolean flag1 = ub.verifyName(username);
		if(flag1){
			int gpques = Integer.parseInt(gpQues);
			boolean flag2 = ub.isGPQuesRight(username, gpques);
			if(flag2){
				boolean flag3 = ub.isGPAnswRight(username, gpques, gpAnsw);
				if(flag3){
					boolean flag4 = ub.resetPassword(username);
					if(flag4){
						request.setAttribute("message_ok", "密码重置为666666");
						request.getRequestDispatcher("getPassword.jsp").forward(request, response);
					}else{
						request.setAttribute("message_systemerror", "系统错误");
						request.getRequestDispatcher("getPassword.jsp").forward(request, response);
					}
				}else{
					request.setAttribute("message_gpanswnoright", "密码提示问题答案不正确");
					request.getRequestDispatcher("getPassword.jsp").forward(request, response);
				}
			}else{
				request.setAttribute("message_gpquesnoright", "密码提示问题不正确");
				request.getRequestDispatcher("getPassword.jsp").forward(request, response);
			}
		}else{
			request.setAttribute("message_noexist", "该账号不存在");
			request.getRequestDispatcher("getPassword.jsp").forward(request, response);
		}
	}

}
